
/*
 * @include OpenLayers/Map.js
 * @include OpenLayers/Projection.js
 * @include OpenLayers/Layer/XYZ.js
 * @include OpenLayers/Tile/Image.js
 * @include OpenLayers/Control/Navigation.js
 * @include OpenLayers/Control/ZoomBox.js
 * @include OpenLayers/Control/NavigationHistory.js
 * @include GeoExt/data/LayerStore.js
 * @include GeoExt/widgets/MapPanel.js
 * @include GeoExt/widgets/Action.js
 * @include GeoExt/widgets/ZoomSlider.js
 * @include GeoExt/widgets/tips/ZoomSliderTip.js
 * @include GeoExt/widgets/tree/LayerContainer.js
 */

Ext.namespace("App");

var map ;
var capa;
App.obtainMap = (function(){
	return map;
});

App.obtainLayer = (function(){
	return capa;
});

App.layout = (function() {
    /*
     * Private
     */


	var SinglePoint = OpenLayers.Class.create();
    SinglePoint.prototype = OpenLayers.Class.inherit(OpenLayers.Handler.Point, {
        createFeature: function(evt) {
            this.control.layer.removeFeatures(this.control.layer.features);
            OpenLayers.Handler.Point.prototype.createFeature.apply(this, arguments);
        }
    });

    var prueba = function (feature) {
		//alert("entra");
		var coordX = parent.document.getElementById("form:coordenadaX");
		var coordY = parent.document.getElementById("form:coordenadaY");
		coordX.value = draw.handler.point.geometry.x;
		coordY.value = draw.handler.point.geometry.y;

		//alert(draw.handler.point.geometry.x+", "+draw.handler.point.geometry.y);
    }


	var start_style = OpenLayers.Util.applyDefaults({
        //externalGraphic: "start.png",
		strokeColor : "blue",
        fillColor : "blue",
        graphicWidth: 18,
        graphicHeight: 26,
        graphicYOffset: -26,
        graphicOpacity: 1,
        fillOpacity : 1
    }, OpenLayers.Feature.Vector.style['default']);

    var stop_style = OpenLayers.Util.applyDefaults({
        //externalGraphic: "stop.png",
    	strokeColor : "red",
        fillColor : "red",
        graphicWidth: 18,
        graphicHeight: 26,
        graphicYOffset: -26,
        graphicOpacity: 1,
        fillOpacity : 1
    }, OpenLayers.Feature.Vector.style['default']);


	var start = new OpenLayers.Layer.Vector("Start point",{style: start_style});
	capa = start;
	var prueba1 = function () {
		//alert("prueba1 " + start);
		//alert("prueba1 " + start.features);
		start.removeFeatures(start.features);

	}
	start.events.register("beforefeatureadded", start, prueba1);
	/*start.events.register("beforefeaturesadded", start, function (feature) {
		alert("111 " + feature );
		);*/
	var stop = new OpenLayers.Layer.Vector("End point",{style: stop_style});
	var draw = new OpenLayers.Control.DrawFeature(start, OpenLayers.Handler.Point);
	draw.events.register('featureadded', draw,
    		prueba);
	//stop.events.register("beforefeatureadded", stop, prueba1);
	/*stop.events.register("beforefeaturesadded", stop, function (feature) {
		alert("111 " + feature );
		);
	*/
    /**
     * Method: createMap
     * Create the map.
     *
     * Returns:
     * {OpenLayers.Map} The OpenLayers.Map instance.
     */
    var createMap = function() {

        return new OpenLayers.Map('map',
			{
            maxResolution:'auto',
            //maxExtent: new OpenLayers.Bounds(-81850, 3909222, 1170200, 4850397),		///Espaáa
            maxExtent: new OpenLayers.Bounds(96500,3960000,622000,4300000),	////Andalucia
			units: 'm',
            projection: 'EPSG:23030',
             minScale: 2854694.4371093751
            });
    	//return new OpenLayers.Map('map');
    };

    /**
     * Method: createLayers
     * Create the layers.
     *
     * Returns:
     * {Array({OpenLayers.Layer}) Array of layers.
     */
    var createLayers = function() {
        return [
                new OpenLayers.Layer.WMS("Municipios", "http://www.ideandalucia.es/wms/mta10v_2007?", {layers: 'Limites',transparent: true},{isBaseLayer: true, visibility:false,singleTile: true,ratio: 1}),
                new OpenLayers.Layer.WMS("Callejero", "http://www.juntadeandalucia.es/servicios/mapas/callejero/wms?", {layers: 'callejeroCompleto',transparent: true},{isBaseLayer: false, visibility:true,singleTile: true,ratio: 1}),
                start

        ];
    };

    /**
     * Method: createLayerStore
     * Create a GeoExt layer store.
     *
     * Parameters:
     * map - {OpenLayers.Map} The Map instance.
     * layers - {Array({OpenLayers.Layer})} The layers to add to the store.
     *
     * Returns:
     * {GeoExt.data.LayerStore} The layer store.
     *
     */
    var createLayerStore = function(map, layers) {
        return new GeoExt.data.LayerStore({
            map: map,
            layers: layers
        });
    };

    /**
     * Method: createTbarItems
     * Create map toolbar items
     *
     * Returns:
     * {Array({GeoExt.Action})} An array of GeoExt.Action objects.
     */
    var createTbarItems = function(map) {
        var actions = [];
        actions.push(new GeoExt.Action({
            iconCls: "pan",
            map: map,
            pressed: true,
            toggleGroup: "tools",
            allowDepress: false,
            tooltip: "Navigate",
            control: new OpenLayers.Control.Navigation()
        }));
        actions.push(new GeoExt.Action({
            iconCls: "zoomin",
            map: map,
            toggleGroup: "tools",
            allowDepress: false,
            tooltip: "Zoom in",
            control: new OpenLayers.Control.ZoomBox({
                out: false
            })
        }));
        var ctrl = new OpenLayers.Control.NavigationHistory();
        map.addControl(ctrl);
        actions.push(new GeoExt.Action({
            control: ctrl.previous,
            iconCls: "back",
            tooltip: "back",
            disabled: true
        }));
        actions.push(new GeoExt.Action({
            control: ctrl.next,
            iconCls: "next",
            tooltip: "next",
            disabled: true
        }));

       // alert(start);
        //var draw = new OpenLayers.Control.DrawFeature(start, OpenLayers.Handler.Point);
        //map.addControl(draw);




        actions.push(new GeoExt.Action({
            iconCls: "centrar",
            map: map,
            toggleGroup: "tools",
            allowDepress: false,
            tooltip: "prueba",
            control: draw
        }));

        var draw1 = new OpenLayers.Control.DrawFeature(stop, SinglePoint);
        //map.addControl(draw);
       /* actions.push(new GeoExt.Action({
            iconCls: "zoomin",
            map: map,
            toggleGroup: "tools",
            allowDepress: false,
            tooltip: "prueba2",
            control: draw1
        }));*/


        return actions;
    };


    
    


    /*
     * Public
     */
    return {

        /**
         * APIMethod: init
         * Initialize the page layout.
         */
        init: function() {

            map = createMap();

           // alert(start);
            var layers = createLayers();
            var layerStore = createLayerStore(map, layers);

            new Ext.Viewport({
                layout: "border",
                items: [{
                    title: "Map",
                    region: "center",
                    xtype: "gx_mappanel",
                    map: map,
                    layers: layerStore,
                    zoom: 4,
                    width: 600,
                    tbar: createTbarItems(map)
                }, {
                    title: "Layer tree",
                    region: "west",
                    width: 150,
                    xtype: "treepanel",
                    collapsible:true,
                    loader: new Ext.tree.TreeLoader({
                        applyLoader: false
                    }),
                    root: {
                        nodeType: "gx_layercontainer",
                        layerStore: layerStore,
                        leaf: false,
                        expanded: true
                    },
                    rootVisible: false
                }]
            });

            map.zoomToMaxExtent();
            parent.centrarPintar(map, start);
        }
    };
})();
